import AniBox from '../libs/AniBox.esm.js'
import { zrender } from '../libs/zrender-helper.js'
import store from '../store/index.js'
import { convertPosition } from './main.js'
import { createShoot } from './shoot.js'
import SAT from '../libs/SAT.js'

const init = () => {
  let { main,stage } = store.state;
  let CONFIG = store.state.config.player;
  let player = new AniBox({
    data(){
      return {
        x : 100,
        y : 0,
        w : CONFIG.w,
        h : CONFIG.h,
        shoots : [],
        moveInfo : {
          start : false,
          x : 0,
        }
      }
    },
    methods : {
      shoot(){
        let shoot = createShoot(this);
        this.addShoot(shoot);
        //console.log(this.shoots.length);
      },
      addShoot(shoot){
        this.add(shoot);
        this.shoots.push(shoot);
      },
      removeShoot(shoot){
        this.remove(shoot);
        let index = this.shoots.indexOf(shoot);
        this.shoots.splice(index,1);
      },
      autoShoot(){
        this.intervalTime(CONFIG.shoot.itime,res => this.shoot());
      },
      checkEnemy(){
        let player_position = convertPosition([this.x,this.y]);
        let player = new SAT.Box(new SAT.Vector(player_position[0],player_position[1]),this.w,this.h);
        let { list } = store.state.enemy;
        list.forEach(ene => {
          let ene_position = convertPosition([ene.x,ene.y]);
          if(ene_position[1] + ene.h >= player_position[1]){
            let eneSAT = new SAT.Box(new SAT.Vector(ene_position[0],ene_position[1]),ene.w,ene.h);
            let res = SAT.testPolygonPolygon(player.toPolygon(),eneSAT.toPolygon());
            if(res){
              //stage.end();
            }
          }
        });
      },
      left(){
        let x = this.x;
        x = x - 10;
        x = x < 0 ? 0 : x;
        this.x = x;
      },
      right(){
        let x = this.x;
        x = x + 10;
        x = x > CONFIG.mr ? CONFIG.mr : x;
        this.x = x;
      },
      setX(newX){
        let x = newX < 0 ? 0 : newX;
        x = x > CONFIG.mr ? CONFIG.mr : x;
        this.x = newX;
      },
      _move0(e){
        this.moveInfo = {
          start : true,
          zrX : e.event.zrX,
          x : this.x
        }
      },
      _move1(e){
        if(this.moveInfo.start){
          let zrX = e.event.zrX;
          let diffX = zrX - this.moveInfo.zrX;
          let newX = this.moveInfo.x + diffX;
          this.setX(newX);
        }
      },
      _move2(){
        this.moveInfo.start = false;
      }
    },
    created(){
      let shape = new zrender.Rect({
        shape : {
          width : this.w,
          height : this.h,
        },
        style : {
          fill : 'red',
        },
      });
      shape.on('mousedown',res => {
        console.log(`player,mousedown`);
        this._move0(res);
      });
      main.on('mouseup',res => {
        console.log(`player,mouseup`);
        this._move2(res);
      });
      main.on('mousemove',res => {
        console.log(`player,mousemove`);
        this._move1(res);
      });
      main.add(shape);
      this.shape = shape;
      this.autoShoot();
    },
    step(){
      this.checkEnemy();
    },
    render(){
      this.shape.attr({
        position : convertPosition([this.x,this.y]),
      })
    }
  });
  stage.add(player);
  store.state.player = player;
  return player;
}


export { init };
